import { Bezier } from './Bezier'


export class BezierData {

    // 贝塞尔曲线列表
    bezierCurveLists = [];
    // 曲线点列表
    bezierCurveData = {
        time: 2,//运行总时长
        length: 0,//曲线总长
        points: [],//曲线点列表
    }
    // // 点 - 曲线 字典
    pointCurveDict = new Map();
    // 需要删除的目标节点
    deleteTarget = null;
    // 点计数
    PointNum = 0;
    // 曲线类型
    currentBezierType = 2;
    // 每段曲线的切割份数
    pointCount = 100;


    // ------------------------【公有方法】---------------------------
    // 设置曲线切割份数
    setPointCount (num) {
        this.pointCount = num;
    }
    // 设置曲线类型
    setBezierCurveType (type) {
        this.currentBezierType = type;
    }
    // 设置删除的目标节点
    setDeleteTarget (node) {
        this.deleteTarget = node;
    }
    // 添加贝塞尔曲线到列表
    addBezierCurve (curve) {
        this.bezierCurveLists.push(curve);
        // console.log("bezierCurveLists", this.bezierCurveLists);
    }

    // 获取曲线类型
    getBezierCurveType () {
        return this.currentBezierType;
    }
    // 获取贝塞尔曲线列表
    getBezierCurveLists () {
        return this.bezierCurveLists;
    }

    // 获取贝塞尔曲线数据
    getBezierCurveData () {
        return this.bezierCurveData;
    }

    // 设置贝塞尔曲线运行时长
    setBezierCurveRunTime (time) {
        this.bezierCurveData.time = time;
    }
    // 是否是最后一个曲线
    isLastCurve () {
        return this.bezierCurveLists.length <= 1;
    }


    // 存储到曲线字典
    // key - 点, value - 该点所关联的曲线对象Obj, 
    // 曲线对象Obj: start字段 为 该点作为起点所在的曲线,  control end类似
    saveToPointCurveDict (curve) {
        let obj;
        for (const key in curve) {
            const point = curve[key];
            if (this.pointCurveDict.has(point)) {
                obj = this.pointCurveDict.get(point);
            } else {
                obj = {};
            }
            obj[key + "Curve"] = curve;
            this.pointCurveDict.set(point, obj);
        }
        console.log("pointCurveDict", this.pointCurveDict);
    }


    // 保存路径
    saveBezierPath () {
        this.bezierCurveData.length = 0;
        this.bezierCurveData.points = [];
        // console.log("保存路径bezierLists", this.bezierCurveLists);
        for (var i = 0, len = this.bezierCurveLists.length; i < len; i++) {
            const bezier = this.bezierCurveLists[i];
            // 创建一个贝塞尔曲线
            // let bezierCurve = new Bezier(bezier.start, bezier.control, bezier.end, 100);
            // console.log("consscscds", Object.values(bezier));

            let bezierCurve = new Bezier(Object.values(bezier), 2);

            // 获取曲线点
            let points = bezierCurve.getPoints(this.pointCount);

            // 获取曲线长度
            let curveLength = bezierCurve.getCurveLength();
            // 计算路程长度
            this.bezierCurveData.length += curveLength;
            // 存储曲线点
            this.bezierCurveData.points.push(...points);
            // console.log("points", points);
        }
    }

    // 情况所有曲线
    clearAllBezier () {
        this.bezierCurveLists = [];
        this.pointCurveDict.forEach((curve, point) => {
            if (point)
                point.destroy();
        })
        this.pointCurveDict.clear()
    }

    // ------------------------【私有方法】---------------------------


    // 初始化一个随机曲线
    initRandCurve(bezier) {

        this.addBezierCurve(bezier);
        this.saveToPointCurveDict(bezier);
    }


}